********************************************************************************
** 	TITLE:		nz2002_nzes_contact_networks                                  ** 	
**  AUTHOR:	    Philippe Mongrain                                             **
**	DATA:       pre_post_2002rel                                              **
**  DATE:	    October 2022 						                          **	
**	VERSION:	Stata 16					                                  **	
********************************************************************************

* Version control

version 16.0

* Open log file

capture log close       			  			              
log using "nz2002_nzes_contact_networks", replace

* Open the dataset

use "pre_post_2002rel.dta", clear


**************************
** RELABELING DISTRICTS **
**************************

gen district = .

replace district = 1 if el_2_nm == "Aoraki"
replace district = 2 if el_2_nm == "Auckland Central"
replace district = 3 if el_2_nm == "Banks Peninsula"
replace district = 4 if el_2_nm == "Bay of Plenty"
replace district = 5 if el_2_nm == "Christchurch Cen"
replace district = 6 if el_2_nm == "Christchurch Eas"
replace district = 7 if el_2_nm == "Clevedon"
replace district = 8 if el_2_nm == "Clutha-Southland"
replace district = 9 if el_2_nm == "Coromandel"
replace district = 10 if el_2_nm == "Dunedin North"
replace district = 11 if el_2_nm == "Dunedin South"
replace district = 12 if el_2_nm == "East Coast"
replace district = 13 if el_2_nm == "East Coast Bays"
replace district = 14 if el_2_nm == "Epsom"
replace district = 15 if el_2_nm == "Hamilton East"
replace district = 16 if el_2_nm == "Hamilton West"
replace district = 17 if el_2_nm == "Helensville"
replace district = 18 if el_2_nm == "Hutt South"
replace district = 19 if el_2_nm == "Ikaroa-Rawhiti"
replace district = 20 if el_2_nm == "Ilam"
replace district = 21 if el_2_nm == "Invercargill"
replace district = 22 if el_2_nm == "Kaikoura"
replace district = 23 if el_2_nm == "Mana"
replace district = 24 if el_2_nm == "Mangere"
replace district = 25 if el_2_nm == "Manukau East"
replace district = 26 if el_2_nm == "Manurewa"
replace district = 27 if el_2_nm == "Maungakiekie"
replace district = 28 if el_2_nm == "Mt Albert"
replace district = 29 if el_2_nm == "Mt Roskill"
replace district = 30 if el_2_nm == "Napier"
replace district = 31 if el_2_nm == "Nelson"
replace district = 32 if el_2_nm == "New Lynn"
replace district = 33 if el_2_nm == "New Plymouth"
replace district = 34 if el_2_nm == "North Shore"
replace district = 35 if el_2_nm == "Northcote"
replace district = 36 if el_2_nm == "Northland"
replace district = 37 if el_2_nm == "Ohariu-Belmont"
replace district = 38 if el_2_nm == "Otago"
replace district = 39 if el_2_nm == "Otaki"
replace district = 40 if el_2_nm == "Pakuranga"
replace district = 41 if el_2_nm == "Palmerston North"
replace district = 42 if el_2_nm == "Piako"
replace district = 43 if el_2_nm == "Port Waikato"
replace district = 44 if el_2_nm == "Rakaia"
replace district = 45 if el_2_nm == "Rangitikei"
replace district = 46 if el_2_nm == "Rimutaka"
replace district = 47 if el_2_nm == "Rodney"
replace district = 48 if el_2_nm == "Rongotai"
replace district = 49 if el_2_nm == "Rotorua"
replace district = 50 if el_2_nm == "Tainui"
replace district = 51 if el_2_nm == "Tamaki"
replace district = 52 if el_2_nm == "Tamaki Makaurau"
replace district = 53 if el_2_nm == "Taranaki-King Co"
replace district = 54 if el_2_nm == "Taupo"
replace district = 55 if el_2_nm == "Tauranga"
replace district = 56 if el_2_nm == "Te Atatu"
replace district = 57 if el_2_nm == "Te Tai Hauauru"
replace district = 58 if el_2_nm == "Te Tai Tokerau"
replace district = 59 if el_2_nm == "Te Tai Tonga"
replace district = 60 if el_2_nm == "Tukituki"
replace district = 61 if el_2_nm == "Waiariki"
replace district = 62 if el_2_nm == "Waimakariri"
replace district = 63 if el_2_nm == "Wairarapa"
replace district = 64 if el_2_nm == "Waitakere"
replace district = 65 if el_2_nm == "Wellington Centr"
replace district = 66 if el_2_nm == "West Coast-Tasma"
replace district = 67 if el_2_nm == "Whanganui"
replace district = 68 if el_2_nm == "Whangarei"
replace district = 69 if el_2_nm == "Wigram"

label define district 1 "Aoraki" 2 "Auckland Central" 3 "Banks Peninsula" 4 "Bay of Plenty" 5 "Christchurch Central" 6 "Christchurch East" 7 "Clevedon" 8 "Clutha-Southland" 9 "Coromandel" 10 "Dunedin North" 11 "Dunedin South" 12 "East Coast" 13 "East Coast Bays" 14 "Epsom" 15 "Hamilton East" 16 "Hamilton West" 17 "Helensville" 18 "Hutt South" 19 "Ikaroa-Rawhiti" 20 "Ilam" 21 "Invercargill" 22 "Kaikoura" 23 "Mana" 24 "Mangere" 25 "Manukau East" 26 "Manurewa" 27 "Maungakiekie" 28 "Mt Albert" 29 "Mt Roskill" 30 "Napier" 31 "Nelson" 32 "New Lynn" 33 "New Plymouth" 34 "North Shore" 35 "Northcote" 36 "Northland" 37 "Ohariu-Belmont" 38 "Otago" 39 "Otaki" 40 "Pakuranga" 41 "Palmerston North" 42 "Piako" 43 "Port Waikato" 44 "Rakaia" 45 "Rangitikei" 46 "Rimutaka" 47 "Rodney" 48 "Rongotai" 49 "Rotorua" 50 "Tainui" 51 "Tamaki" 52 "Tamaki Makaurau" 53 "Taranaki-King Country" 54 "Taupo" 55 "Tauranga" 56 "Te Atatu" 57 "Te Tai Hauauru" 58 "Te Tai Tokerau" 59 "Te Tai Tonga" 60 "Tukituki" 61 "Waiariki" 62 "Waimakariri" 63 "Wairarapa" 64 "Waitakere" 65 "Wellington Central" 66 "West Coast-Tasman" 67 "Whanganui" 68 "Whangarei" 69 "Wigram"

label values district district


***********************
** DISTRICT OUTCOMES **
***********************

* Note: 1 = ACT, 2 = Alliance, 3 = Christian Heritage, 4 = Green, 5 = Jim Anderton's Progressive Coalition, 6 = Labour, 7 = National, 8 = New Zealand First, 9 = United Future, 37 = other party

gen district_outcome = .

replace district_outcome = 6 if district == 1
replace district_outcome = 6 if district == 2
replace district_outcome = 6 if district == 3
replace district_outcome = 7 if district == 4
replace district_outcome = 6 if district == 5
replace district_outcome = 6 if district == 6
replace district_outcome = 7 if district == 7
replace district_outcome = 7 if district == 8
replace district_outcome = 7 if district == 9
replace district_outcome = 6 if district == 10
replace district_outcome = 6 if district == 11
replace district_outcome = 6 if district == 12
replace district_outcome = 7 if district == 13
replace district_outcome = 7 if district == 14
replace district_outcome = 6 if district == 15
replace district_outcome = 6 if district == 16
replace district_outcome = 7 if district == 17
replace district_outcome = 6 if district == 18
replace district_outcome = 6 if district == 19
replace district_outcome = 7 if district == 20
replace district_outcome = 6 if district == 21
replace district_outcome = 7 if district == 22
replace district_outcome = 6 if district == 23
replace district_outcome = 6 if district == 24
replace district_outcome = 6 if district == 25
replace district_outcome = 6 if district == 26
replace district_outcome = 6 if district == 27
replace district_outcome = 6 if district == 28
replace district_outcome = 6 if district == 29
replace district_outcome = 6 if district == 30
replace district_outcome = 7 if district == 31
replace district_outcome = 6 if district == 32
replace district_outcome = 6 if district == 33
replace district_outcome = 7 if district == 34
replace district_outcome = 6 if district == 35
replace district_outcome = 7 if district == 36
replace district_outcome = 9 if district == 37
replace district_outcome = 6 if district == 38
replace district_outcome = 6 if district == 39
replace district_outcome = 7 if district == 40
replace district_outcome = 6 if district == 41
replace district_outcome = 7 if district == 42
replace district_outcome = 7 if district == 43
replace district_outcome = 7 if district == 44
replace district_outcome = 7 if district == 45
replace district_outcome = 6 if district == 46
replace district_outcome = 7 if district == 47
replace district_outcome = 6 if district == 48
replace district_outcome = 6 if district == 49
replace district_outcome = 6 if district == 50
replace district_outcome = 7 if district == 51
replace district_outcome = 6 if district == 52
replace district_outcome = 7 if district == 53
replace district_outcome = 6 if district == 54
replace district_outcome = 8 if district == 55
replace district_outcome = 6 if district == 56
replace district_outcome = 6 if district == 57
replace district_outcome = 6 if district == 58
replace district_outcome = 6 if district == 59
replace district_outcome = 6 if district == 60
replace district_outcome = 6 if district == 61
replace district_outcome = 6 if district == 62
replace district_outcome = 6 if district == 63
replace district_outcome = 6 if district == 64
replace district_outcome = 6 if district == 65
replace district_outcome = 6 if district == 66
replace district_outcome = 6 if district == 67
replace district_outcome = 7 if district == 68
replace district_outcome = 5 if district == 69


*********************
** COMPETITIVENESS **
*********************

* Margin of victory by district

gen margin = .

replace margin = 20.44206 if district == 1
replace margin = 28.3032 if district == 2
replace margin = 18.63697 if district == 3
replace margin = 7.601843 if district == 4
replace margin = 27.17582 if district == 5
replace margin = 37.41103 if district == 6
replace margin = 9.469999 if district == 7
replace margin = 8.874504 if district == 8
replace margin = 12.0167 if district == 9
replace margin = 33.52394 if district == 10
replace margin = 40.61346 if district == 11
replace margin = 18.90377 if district == 12
replace margin = 8.973957 if district == 13
replace margin = .5264606 if district == 14
replace margin = 11.93598 if district == 15
replace margin = 20.62058 if district == 16
replace margin = 5.021091 if district == 17
replace margin = 26.83481 if district == 18
replace margin = 47.67804 if district == 19
replace margin = 3.373144 if district == 20
replace margin = 20.1247 if district == 21
replace margin = 12.73529 if district == 22
replace margin = 30.24717 if district == 23
replace margin = 63.48573 if district == 24
replace margin = 40.06343 if district == 25
replace margin = 44.05214 if district == 26
replace margin = 33.55972 if district == 27
replace margin = 38.99157 if district == 28
replace margin = 32.5807 if district == 29
replace margin = 26.96999 if district == 30
replace margin = 25.59829 if district == 31
replace margin = 33.68114 if district == 32
replace margin = 19.71631 if district == 33
replace margin = 3.848373 if district == 34
replace margin = 17.2416 if district == 35
replace margin = 6.740074 if district == 36
replace margin = 12.26496 if district == 37
replace margin = 13.99199 if district == 38
replace margin = 23.52014 if district == 39
replace margin = 5.867136 if district == 40
replace margin = 21.02174 if district == 41
replace margin = 5.365353 if district == 42
replace margin = .8058758 if district == 43
replace margin = 3.070324 if district == 44
replace margin = 8.321333 if district == 45
replace margin = 29.65271 if district == 46
replace margin = 9.480846 if district == 47
replace margin = 28.01277 if district == 48
replace margin = 17.20003 if district == 49
replace margin = 38.79701 if district == 50
replace margin = 6.749985 if district == 51
replace margin = 40.94479 if district == 52
replace margin = 5.362831 if district == 53
replace margin = 17.69202 if district == 54
replace margin = 10.09427 if district == 55
replace margin = 38.55558 if district == 56
replace margin = 43.41605 if district == 57
replace margin = 26.73255 if district == 58
replace margin = 38.30334 if district == 59
replace margin = 17.91134 if district == 60
replace margin = 36.3989 if district == 61
replace margin = 20.03836 if district == 62
replace margin = 13.54568 if district == 63
replace margin = 32.83821 if district == 64
replace margin = 16.92939 if district == 65
replace margin = 12.33401 if district == 66
replace margin = 16.19655 if district == 67
replace margin = 12.05017 if district == 68
replace margin = 28.39 if district == 69

* Effective number of electoral parties by district

gen enep = .

replace enep = 3.653907 if district == 1
replace enep = 3.863722 if district == 2
replace enep = 4.250134 if district == 3
replace enep = 4.693691 if district == 4
replace enep = 3.984054 if district == 5
replace enep = 3.306596 if district == 6
replace enep = 4.599731 if district == 7
replace enep = 3.567165 if district == 8
replace enep = 4.679545 if district == 9
replace enep = 3.367286 if district == 10
replace enep = 2.86434 if district == 11
replace enep = 4.119158 if district == 12
replace enep = 4.702983 if district == 13
replace enep = 4.512089 if district == 14
replace enep = 4.605574 if district == 15
replace enep = 4.06414 if district == 16
replace enep = 4.933995 if district == 17
replace enep = 3.782355 if district == 18
replace enep = 2.567115 if district == 19
replace enep = 4.425589 if district == 20
replace enep = 3.324732 if district == 21
replace enep = 4.085084 if district == 22
replace enep = 3.599736 if district == 23
replace enep = 1.937514 if district == 24
replace enep = 2.802669 if district == 25
replace enep = 2.825736 if district == 26
replace enep = 3.273768 if district == 27
replace enep = 3.198014 if district == 28
replace enep = 3.360824 if district == 29
replace enep = 3.778889 if district == 30
replace enep = 3.843323 if district == 31
replace enep = 3.535798 if district == 32
replace enep = 4.02399 if district == 33
replace enep = 4.703878 if district == 34
replace enep = 4.399581 if district == 35
replace enep = 5.141169 if district == 36
replace enep = 4.387586 if district == 37
replace enep = 3.95136 if district == 38
replace enep = 3.831053 if district == 39
replace enep = 4.69081 if district == 40
replace enep = 4.007071 if district == 41
replace enep = 4.68738 if district == 42
replace enep = 4.987574 if district == 43
replace enep = 4.293304 if district == 44
replace enep = 4.358342 if district == 45
replace enep = 3.730025 if district == 46
replace enep = 4.939996 if district == 47
replace enep = 3.837357 if district == 48
replace enep = 4.484262 if district == 49
replace enep = 3.087523 if district == 50
replace enep = 4.343822 if district == 51
replace enep = 2.84905 if district == 52
replace enep = 4.592457 if district == 53
replace enep = 4.263969 if district == 54
replace enep = 4.647742 if district == 55
replace enep = 3.244719 if district == 56
replace enep = 2.867679 if district == 57
replace enep = 3.522395 if district == 58
replace enep = 3.313797 if district == 59
replace enep = 4.123672 if district == 60
replace enep = 3.08131 if district == 61
replace enep = 3.933729 if district == 62
replace enep = 4.275143 if district == 63
replace enep = 3.743422 if district == 64
replace enep = 4.497063 if district == 65
replace enep = 5.068762 if district == 66
replace enep = 4.174137 if district == 67
replace enep = 4.605551 if district == 68
replace enep = 3.873928 if district == 69


***************************
** INDEPENDENT VARIABLES **
***************************

* Time

gen surveydate = dofc(day)

format %tdMon_DD,_CCYY surveydate

gen edate = 20020727

gen electiondate=date(string(edate,"%8.0f"),"YMD")

format %tdMon_DD,_CCYY electiondate

gen time = (electiondate - surveydate) - 1 // Substract 1 to correct for election date

* Gender

gen male = csex

recode male (2=0) 

* Age

gen age = kage

recode age (-7997=.)

replace age = . if age < 17

* Education

gen education = wedqual

* Interest for politics

gen interest = cintrst

recode interest (8=.)
recode interest (9=.)

replace interest = 5 - interest

* Party identification (national)

gen pidstatus_whole = .

replace pidstatus_whole = 9999 if wptisan == . & wclose == .

replace pidstatus_whole = 2 if wptisan == 0 & wclose == 0
replace pidstatus_whole = 2 if wptisan == 0 & wclose == .

replace pidstatus_whole = 3 if wptisan == 2 & pidstatus_whole!=2 & pidstatus_whole!=9999 | wptisan == 8 & pidstatus_whole!=2 & pidstatus_whole!=9999
replace pidstatus_whole = 3 if wclose == 1 & pidstatus_whole!=2 & pidstatus_whole!=9999 | wclose == 8 & pidstatus_whole!=2 & pidstatus_whole!=9999

replace pidstatus_whole = 1 if wptisan!=2 & wptisan!=8 & wptisan!=. & pidstatus_whole!=2 & pidstatus_whole!=3 & pidstatus_whole!=9999
replace pidstatus_whole = 1 if wclose!=1 & wclose!=8 & wclose!=. & pidstatus_whole!=2 & pidstatus_whole!=3 & pidstatus_whole!=9999

replace pidstatus_whole = . if pidstatus_whole == 9999

label define pidstatus 1 "Loser PID" 2 "No PID" 3 "Winner PID"
label values pidstatus_whole pidstatus

* Party identification scale (national)

gen pidscale_whole = .

replace pidscale_whole = 1 if pidstatus_whole == 1 & wptystr == 1
replace pidscale_whole = 2 if pidstatus_whole == 1 & wptystr == 2
replace pidscale_whole = 3 if pidstatus_whole == 1 & wptystr == 3
replace pidscale_whole = 4 if pidstatus_whole == 2
replace pidscale_whole = 5 if pidstatus_whole == 3 & wptystr == 3
replace pidscale_whole = 6 if pidstatus_whole == 3 & wptystr == 2
replace pidscale_whole = 7 if pidstatus_whole == 3 & wptystr == 1

label define pidscale 1 "Very strong loser PID" 2 "Fairly strong loser PID" 3 "Not very strong loser PID" 4 "No PID" 5 "Not very strong winner PID" 6 "Fairly strong winner PID" 7 "Very strong winner PID"
label values pidscale_whole pidscale

* Party identification (district)

gen pidstatus_district = .

replace pidstatus_district = 9999 if wptisan == . & wclose == .

replace pidstatus_district = 2 if wptisan == 0 & wclose == 0
replace pidstatus_district = 2 if wptisan == 0 & wclose == .

replace pidstatus_district = 3 if wptisan == 1 & district_outcome == 37
replace pidstatus_district = 3 if wptisan == 2 & district_outcome == 6
replace pidstatus_district = 3 if wptisan == 3 & district_outcome == 7
replace pidstatus_district = 3 if wptisan == 4 & district_outcome == 4
replace pidstatus_district = 3 if wptisan == 5 & district_outcome == 8
replace pidstatus_district = 3 if wptisan == 6 & district_outcome == 1

replace pidstatus_district = 3 if wclose == 1 & district_outcome == 6
replace pidstatus_district = 3 if wclose == 2 & district_outcome == 7
replace pidstatus_district = 3 if wclose == 3 & district_outcome == 4
replace pidstatus_district = 3 if wclose == 4 & district_outcome == 8
replace pidstatus_district = 3 if wclose == 5 & district_outcome == 1
replace pidstatus_district = 3 if wclose == 6 & district_outcome == 37

replace pidstatus_district = 1 if wptisan == 1 & district_outcome!=37 & district_outcome!=. & pidstatus_district!=2 & pidstatus_district!=3
replace pidstatus_district = 1 if wptisan == 2 & district_outcome!=6 & district_outcome!=. & pidstatus_district!=2 & pidstatus_district!=3
replace pidstatus_district = 1 if wptisan == 3 & district_outcome!=7 & district_outcome!=. & pidstatus_district!=2 & pidstatus_district!=3
replace pidstatus_district = 1 if wptisan == 4 & district_outcome!=4 & district_outcome!=. & pidstatus_district!=2 & pidstatus_district!=3
replace pidstatus_district = 1 if wptisan == 5 & district_outcome!=8 & district_outcome!=. & pidstatus_district!=2 & pidstatus_district!=3
replace pidstatus_district = 1 if wptisan == 6 & district_outcome!=1 & district_outcome!=. & pidstatus_district!=2 & pidstatus_district!=3

replace pidstatus_district = 1 if wclose == 1 & district_outcome!=6 & district_outcome!=. & pidstatus_district!=2 & pidstatus_district!=3
replace pidstatus_district = 1 if wclose == 2 & district_outcome!=7 & district_outcome!=. & pidstatus_district!=2 & pidstatus_district!=3
replace pidstatus_district = 1 if wclose == 3 & district_outcome!=4 & district_outcome!=. & pidstatus_district!=2 & pidstatus_district!=3
replace pidstatus_district = 1 if wclose == 4 & district_outcome!=8 & district_outcome!=. & pidstatus_district!=2 & pidstatus_district!=3
replace pidstatus_district = 1 if wclose == 5 & district_outcome!=1 & district_outcome!=. & pidstatus_district!=2 & pidstatus_district!=3
replace pidstatus_district = 1 if wclose == 6 & district_outcome!=37 & district_outcome!=. & pidstatus_district!=2 & pidstatus_district!=3

replace pidstatus_district = . if pidstatus_district == 9999

label values pidstatus_district pidstatus

* Party identification scale (district)

gen pidscale_district = .

replace pidscale_district = 1 if pidstatus_district == 1 & wptystr == 1
replace pidscale_district = 2 if pidstatus_district == 1 & wptystr == 2
replace pidscale_district = 3 if pidstatus_district == 1 & wptystr == 3
replace pidscale_district = 4 if pidstatus_district == 2
replace pidscale_district = 5 if pidstatus_district == 3 & wptystr == 3
replace pidscale_district = 6 if pidstatus_district == 3 & wptystr == 2
replace pidscale_district = 7 if pidstatus_district == 3 & wptystr == 1

label values pidscale_district pidscale

* Party identification (national) - Alternative measure

gen a_pidstatus_whole = .

replace a_pidstatus_whole = 2 if cptisn == 97

replace a_pidstatus_whole = 3 if cptisn == 6

replace a_pidstatus_whole = 1 if cptisn!=6 & cptisn!=98 & cptisn!=99 & cptisn!=.

* Party identification (district) - Alternative measure

gen a_pidstatus_district = .

replace a_pidstatus_district = 2 if cptisn == 97

replace a_pidstatus_district = 3 if cptisn == district_outcome

replace a_pidstatus_district = 1 if cptisn!=district_outcome & cptisn!=98 & cptisn!=99 & cptisn!=.

label values a_pidstatus_district pidstatus

label values a_pidstatus_whole pidstatus

* TV One news attentiveness

gen tv1 = 5 - wtvon

recode tv1 (.=-99)

* TV3 news attentiveness

gen tv3 = 5 - wtvth

recode tv3 (.=-99)

* Press news attentiveness

gen press = 5 - wnews

recode press (.=-99)

* National radio news attentiveness

gen natradio = 5 - wntrd

recode natradio (.=-99)

* Talkback radio news attentiveness

gen talkradio = 5 - wtbrd

recode talkradio (.=-99)

* Internet news attentiveness

gen internet = wintnt

recode internet (1=.)
recode internet (.=-99)

replace internet = 1 if internet == 2
replace internet = 2 if internet == 3
replace internet = 3 if internet == 4
replace internet = 4 if internet == 5

* News attentiveness index

egen news = rowmax(tv1 tv3 press natradio talkradio internet)

recode news (-99=.)

* Party or parties that formed the government after the 1999 election - Correct answers (Labour and Alliance) = 1, Incorrect answer = 0

gen gov1999_dk = wptyrccn
gen gov1999_labour = wptyrclb
gen gov1999_national = wptyrcnt
gen gov1999_nzfirst = wptyrcnz
gen gov1999_green = wptyrcgr
gen gov1999_act = wptyrcac
gen gov1999_alliance = wptyrcal
gen gov1999_united = wptyrcun

replace gov1999_dk = -1 if gov1999_dk == 1 // Incorrect answer

replace gov1999_labour = -1 if gov1999_labour == 0 // Incorrect answer
replace gov1999_labour = 1 if gov1999_labour == 1 // Correct answer

replace gov1999_national = 0 if gov1999_national == 0 // Correct answer
replace gov1999_national = -1 if gov1999_national == 1 // Incorrect answer

replace gov1999_nzfirst = 0 if gov1999_nzfirst == 0 // Correct answer
replace gov1999_nzfirst = -1 if gov1999_nzfirst == 1 // Incorrect answer

replace gov1999_green = 0 if gov1999_green == 0 // Correct answer
replace gov1999_green = -1 if gov1999_green == 1 // Incorrect answer

replace gov1999_act = 0 if gov1999_act == 0 // Correct answer
replace gov1999_act = -1 if gov1999_act == 1 // Incorrect answer

replace gov1999_alliance = -1 if gov1999_alliance == 0 // Correct answer
replace gov1999_alliance = 1 if gov1999_alliance == 1 // Incorrect answer

replace gov1999_united = 0 if gov1999_united == 0 // Correct answer
replace gov1999_united = -1 if gov1999_united == 1 // Incorrect answer

recode gov1999_dk gov1999_labour gov1999_national gov1999_nzfirst gov1999_green gov1999_act gov1999_alliance gov1999_united (.=0)

gen gov1999 = gov1999_dk + gov1999_labour + gov1999_national + gov1999_nzfirst + gov1999_green + gov1999_act + gov1999_alliance + gov1999_united

replace gov1999 = 0 if gov1999 < 0

* The term of Parliament is four years - Correct answers (false) = 1, Incorrect answer = 0

gen termlength = wquiz1

replace termlength = 0 if termlength == 1 // Incorrect answer
replace termlength = 0 if termlength == 9 // Incorrect answer
replace termlength = 1 if termlength == 2 // Correct answer

* Enrolling as a voter in New Zealand is compulsory - Correct answers (true) = 1, Incorrect answer = 0

gen enrollment = wquiz2

replace enrollment = 0 if enrollment == 2 // Incorrect answer
replace enrollment = 0 if enrollment == 9 // Incorrect answer
replace enrollment = 1 if enrollment == 1 // Correct answer

* Since 1981 National has been in government for at least five more years than Labour - Correct answers (false) = 1, Incorrect answer = 0

gen fiveyears = wquiz3

replace fiveyears = 0 if fiveyears == 1 // Incorrect answer
replace fiveyears = 0 if fiveyears == 9 // Incorrect answer
replace fiveyears = 1 if fiveyears == 2 // Correct answer

* Current government has more than half or less than half of the seats in parliament - Correct answers (less than half) = 1, Incorrect answer = 0

gen half = chalf

recode half (8=.)

replace half = 0 if half!=2 & half!=. // Incorrect answer
replace half = 1 if half == 2 // Correct answer

* Which vote will be the most important in deciding which party gets the largest number of seats in parliament - Correct answers (party vote) = 1, Incorrect answer = 0

gen twovotes = cwhcvt

recode twovotes (8=.)

replace twovotes = 0 if twovotes!=1 & twovotes!=. // Incorrect answer
replace twovotes = 1 if twovotes == 1 // Correct answer

* Voting under MMP is like two separate elections, one for the electorate seats and one for the party list seats - Correct answers (false) = 1, Incorrect answer = 0

gen twoelec = wspelec

recode twoelec (8=.)

replace twoelec = 0 if twoelec!=2 & twoelec!=. // Incorrect answer
replace twoelec = 1 if twoelec == 2 // Correct answer

* The party votes usually decide the total number of seats each party gets in Parliament - Correct answers (true) = 1, Incorrect answer = 0

gen partyvote = wpvtdc

recode partyvote (8=.)

replace partyvote = 0 if partyvote!=1 & partyvote!=. // Incorrect answer
replace partyvote = 1 if partyvote == 1 // Correct answer

* The party with the most votes is more likely to get the most seats under MMP than under first past the post - Correct answers (true) = 1, Incorrect answer = 0

gen mmpfptp = wmsvtst

recode mmpfptp (8=.)

replace mmpfptp = 0 if mmpfptp!=1 & mmpfptp!=. // Incorrect answer
replace mmpfptp = 1 if mmpfptp == 1 // Correct answer

* Political knowledge index

gen knowledge = gov1999 + termlength + enrollment + fiveyears + half + twoelec + partyvote + mmpfptp

* Political discussion

gen discussion = cdisc

recode discussion (9=.)

replace discussion = 3 - discussion


*********************************
** NATIONAL-LEVEL EXPECTATIONS **
*********************************

* Labour or National in government after election

gen dk = . // Generate variable for "don't know" answers

replace dk = 1 if cglknt == 9 | cglklb == 9 // Generate value of 1 if the respondent does not know either for Labour or National

gen national = cglknt
gen labour = cglklb

recode national labour (8=.) // Recode "refused" as missing data
recode national labour (9=.) // Recode "don't know" answers as missing data

replace national = 6 - national // Chance National will be in government after election
replace labour = 6 - labour // Chance Labour will be in government after election

gen correct_whole_d = 1 if labour > national & labour!=. & national!=. // Correct answer if chance for Labour is greater than chance for National
replace correct_whole_d = 0 if correct_whole_d!=1 & labour!=. & national!=. // Incorrect answer if chance for Labour is not greater than chance for National
replace correct_whole_d = 0 if dk == 1 // "Don't know" answers coded as incorrect

* Expected number of seats

destring cxstac, gen(act_seats) // Expected number of seats for ACT
destring cxsgr, gen(green_seats) // Expected number of seats for Green Party

gen labour_seats = cxslb // Expected number of seats for Labour
gen national_seats = cxsnt // Expected number of seats for National
gen nzfirst_seats = cxsnz // Expected number of seats for NZ First

gen forecast_seats_act = abs(act_seats - 9) // Difference between expected number of seats of actual number of seats for ACT
gen forecast_seats_green = abs(green_seats - 9) // Difference between expected number of seats of actual number of seats for Green Party
gen forecast_seats_labour = abs(labour_seats - 52) // Difference between expected number of seats of actual number of seats for Labour Party
gen forecast_seats_national = abs(national_seats - 27) // Difference between expected number of seats of actual number of seats for National Party
gen forecast_seats_nzfirst = abs(nzfirst_seats - 13) // Difference between expected number of seats of actual number of seats for NZ First

gen forecast_seats = (forecast_seats_act + forecast_seats_green + forecast_seats_labour + forecast_seats_national + forecast_seats_nzfirst)/5 // Average error for all parties

gen forecast_seats_labnat = (forecast_seats_labour + forecast_seats_national)/2 // Average error for Labour Party and National Party


*********************************
** DISTRICT-LEVEL EXPECTATIONS ** 
*********************************

* Forecasts (1 = ACT, 2 = Alliance, 3 = Christian Heritage, 4 = Green, 5 = Jim Anderton's Progressive Coalition, 6 = Labour, 7 = National, 8 = New Zealand First, 9 = United Future, 37 = other party)

gen forecast_district = anwn

recode forecast_district (98=.) 

* Identify correct forecasts

gen correct_district_d = 0 if forecast_district!=district_outcome & forecast_district!=.
replace correct_district_d = 1 if forecast_district == district_outcome & forecast_district!=.


**********
** SAVE **
**********

save "nz2002_nzes.dta", replace


*******************
** MAIN ANALYSES **
*******************

* Regression analysis (Labour or National in government after election) and percentage of correct and incorrect forecasts

logistic correct_whole_d discussion pidscale_whole interest news knowledge age i.male education time  
estimates table, star(.05 .01 .001)

tab correct_whole_d if e(sample) == 1

* Regression analysis (district forecast) and percentage of correct and incorrect forecasts

melogit correct_district_d discussion pidscale_district interest news knowledge age i.male education time enep || district: , or  
estimates table, star(.05 .01 .001)

tab correct_district_d if e(sample) == 1

* Regression analysis (Labour seats forecast) and percentage of correct and incorrect forecasts

reg forecast_seats_labour discussion pidscale_whole interest news knowledge age i.male education time  
estimates table, star(.05 .01 .001)

tab forecast_seats_labour if e(sample) == 1

* Regression analysis (Labour and National seats forecasts) and percentage of correct and incorrect forecasts

reg forecast_seats_labnat discussion pidscale_whole interest news knowledge age i.male education time  
estimates table, star(.05 .01 .001)

tab forecast_seats_labnat if e(sample) == 1

* Regression analysis (seats forecasts) and percentage of correct and incorrect forecasts

reg forecast_seats discussion pidscale_whole interest news knowledge age i.male education time  
estimates table, star(.05 .01 .001)

tab forecast_seats if e(sample) == 1


************************
** SUMMARY STATISTICS **
************************

label variable correct_whole_d "Labour or National (DV)"
label variable correct_district_d "District winner (DV)"
label variable forecast_seats "Seats (DV)"
label variable forecast_seats_labour "Labour seats (DV)"
label variable forecast_seats_labnat "Labour/National seats (DV)"
label variable discussion "Discussion"
label variable pidscale_whole "National PID"
label variable pidscale_district "District PID"
label variable knowledge "Political knowledge"
label variable interest "Interest"
label variable news "News attentiveness"
label variable age "Age"
label variable male "Gender (male = 1)"
label variable education "Education"
label variable time "Time of interview"
label variable margin "Margin"
label variable enep "ENEP"

quietly logistic correct_whole_d discussion pidscale_whole interest news knowledge age i.male education time  

estpost tabstat correct_whole_d discussion pidscale_whole interest news knowledge age male education time if e(sample) == 1, statistics(n mean p50 sd min max) columns(statistics)

esttab using "nz2002_nzes_summary_national.tex", substitute("\begin{table}[htbp]" "\begin{table}[H]") cells("count(label(N) fmt(%9.0fc)) mean(fmt(%5.2f) label(Mean)) p50(fmt(%5.1f) label(Median)) sd(fmt(%5.1f) label(Std. dev.)) min(fmt(%5.1f) label(Min)) max(fmt(%5.1f) label(Max))") label width(\textwidth) nomtitle nonumber noobs booktabs title("Summary statistics -- 2002 New Zealand general election (NZES), national level") replace

eststo clear

quietly melogit correct_district_d discussion pidscale_district interest news knowledge age i.male education time enep || district: , or

estpost tabstat correct_district_d discussion pidscale_district interest news knowledge age male education time if e(sample) == 1, statistics(n mean p50 sd min max) columns(statistics)

esttab using "nz2002_nzes_summary_district.tex", substitute("\begin{table}[htbp]" "\begin{table}[H]") cells("count(label(N) fmt(%9.0fc)) mean(fmt(%5.2f) label(Mean)) p50(fmt(%5.1f) label(Median)) sd(fmt(%5.1f) label(Std. dev.)) min(fmt(%5.1f) label(Min)) max(fmt(%5.1f) label(Max))") label width(\textwidth) nomtitle nonumber noobs booktabs title("Summary statistics -- 2002 New Zealand general election (NZES), district level") replace

eststo clear

log close